<template>
  <div class="app-container">
    <el-row class="mb8">
      <el-button
        type="primary"
        title="新增"
        size="small"
        @click="addDset(false)"
      >
        <i class="el-icon-plus"></i>新增
      </el-button>
      <el-button
        type="info"
        size="small"
        title="刷新"
        @click="flashDset()"
      >
        <i class="el-icon-refresh" ></i>刷新
      </el-button>
    </el-row>

    <el-table
        :data="tableData"
        @row-click="selectme"
        border>
        <el-table-column label="" width="35" :resizable="false">
          <template slot-scope="scope">
            <el-radio v-model="checked" name="myselect2":label="scope.row.dsetId">&nbsp;</el-radio>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="名称"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="priTable"
          label="主表"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="dsname"
          label="数据源"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="useType"
          label="连接"
        ></el-table-column>
        <el-table-column width="170" :resizable="true">
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="small"
              @click="addDset(true,scope.row.dsetId)"
            >
              <i class="el-icon-edit" ></i>编辑
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="delDset(scope.row.dsetId)"
            >
              <i class="el-icon-delete" ></i>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    <dsetAdd ref="dsetadd" :isupdate="isupdate"></dsetAdd>
  </div>
</template>

<script>
import request from "@/utils/request"
import dsetAdd from "@/views/bi/model/dataSet/dsetAdd";

export default {
  name: "dset",
  data() {
    return {
      tableData: [],
      checked: null,
      isupdate: false
    };
  },
  components: {
    dsetAdd,
  },
  mounted() {
    this.loadData();
  },
  computed: {},
  methods: {
    loadData(){
      request({
        url:"/bi/model/listDataset",
        method:"get",
      }).then((resp)=>{
        this.tableData = resp.rows;
      })
    },
    selectme: function (a, b) {
      this.checked = a.dsetId;
    },
    addDset(isupdate,dsetId){
      this.isupdate = isupdate;
      this.$refs["dsetadd"].addDset(dsetId);
    },
    delDset(dsetId){
      this.$confirm("是否确认删除？","提示",{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        request({
          url:'/bi/model/deleteDset',
          method:"delete",
          params:{dsetId:dsetId}
        }).then(resp=>{
          this.$message({
            type:"success",
            message:"删除成功",
            duration:1000
          })
          this.loadData();
        })
      });
    },
    flashDset(){
      if(!this.checked){
        this.$message({
          message:'未勾选数据',
          type:'error',
          duration:1500
        })
        return;
      }
      let dset = null;
      let ts = this;
      this.tableData.forEach(val=>{
        if(val.dsetId === ts.checked){
          dset = val;
          return false;
        }
      });
      request({
        url:"/bi/model/reloadDset",
        method:"GET",
        params:{dsetId:dset.dsetId, dsid:dset.dsid},
      }).then((resp)=>{
        this.$message({
          message:'刷新成功',
          type:'success',
          duration:1500
        });
      });
    }
  },
};
</script>
